File: /var/www/html/owlcrm/app/Http/Controllers/CommentController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Comment;
use App\Models\Task;
use Illuminate\Support\Facades\Auth;
class CommentController extends Controller
{
public function store(Request $request, $task_id)
{
$request->validate([
'comment' => 'required|string|max:500', // Comment is required and should not exceed 500 characters
], [
'comment.required' => 'This field is required.', // Custom error message
'comment.max' => 'The comment may not be greater than 500 characters.',
]);
$comment = Comment::create([
'task_id' => $task_id,
'user_id' => Auth::id(),
'comment' => $request->comment,
]);
return response()->json([
'success' => true,
'data' => $comment,
]);
}
public function show($task_id)
{
// Fetch the task along with its comments
$task = Task::with('comments')->findOrFail($task_id);
return view('tasks.show', compact('task'));
}
}